package com.hibase.core.mq.redis.util;

import com.alibaba.fastjson.JSON;
import com.hibase.common.constant.RedisKeyConstant;
import com.hibase.core.mq.component.HibaseQueueUtil;
import com.hibase.core.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author chenfeng
 * @version 1.0
 * @date 2021/7/23 13:38
 * @des https://gitee.com/cheershi
 */
@Component
@Slf4j
public class HibaseRedisQueueUtil implements HibaseQueueUtil {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 通用添加redis-queue
     * @param topic
     * @param msg
     */
    @Override
    public void offer(String topic, Object msg){
        this.offer(false,topic,msg);
    }

    @Override
    public void offer(Boolean beFirst, String topic, Object msg) {
        if(beFirst){
            redisUtil.lRightPush(RedisKeyConstant.QUEUE_PREFIX_KEY+topic, JSON.toJSONString(msg));
        }
        else {
            redisUtil.lLeftPush(RedisKeyConstant.QUEUE_PREFIX_KEY+topic, JSON.toJSONString(msg));
        }
        log.info("【Redis-Queue】 插入redis QUEUE {} | 数据 : {}",topic,msg);
    }
}
